<?php

/**
 * vtnsSlot provides basic S.E.O utility methods.
 *
 * @package    vtns
 * @subpackage util
 * @author     Ky Anh Truong <kyanh.truong@vtns.vn>
 * @version    SVN: $Id: vtnsSlot.class.php 29525 2012-08-28 15:00:00Z kyanh $
 */
 
class vtnsSlot
{
	public static function renderTop()
	{
		$_html = '<div id="details-product">';
		
		$slot_top = Doctrine_Core::getTable("Slot")->findOneByDisplayPositionAndParentId(1, 0);
		$slots = Doctrine_Query::create()
					->from("Slot s")
					->where("parent_id = ?", $slot_top->id)
					->addWhere("is_active = 1")
					->limit(3)
					->fetchArray();
		foreach ($slots as $slot)
			$_html .= vtnsSlot::displaySlotBox($slot);
		
		$_html .= '</div>';
		echo $_html;
	}
	
	public static function renderFeatured()
	{
		$sf_culture = sfContext::getInstance()->getUser()->getCulture();
		$_html = '';
		$q = Doctrine_Query::create()
				->from("Slot s")
				->where("s.display_position = 2")
				->addWhere("s.parent_id = 0")
				->orderBy("s.display_order");
		foreach ($q->fetchArray() as $slot)
		{
			$_html .= '<div id="details-product"><h1 class="title">'.(($sf_culture == "vi")?$slot['display_name_vi']:$slot['display_name']).'</h1>';
			$child_slots = Doctrine_Query::create()
					->from("Slot s")
					->where("parent_id = ?", $slot['id'])
					->addWhere("is_active = 1")
					->limit(3)
					->fetchArray();
			foreach ($child_slots as $child_slot)
				$_html .= vtnsSlot::displaySlotBox($child_slot);
				
			$_html .= "</div>";
		}
		
		echo $_html;
	}
	
	public static function renderBottom()
	{
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		$sf_culture = sfContext::getInstance()->getUser()->getCulture();
		$_html = '';
		
		$slot = Doctrine_Core::getTable("Slot")->findOneByDisplayPosition(3);
		$_html .= '<h1 class="title">'.(($sf_culture == "vi")?$slot->display_name_vi:$slot->display_name).'</h1>';
		$category = Doctrine_Core::getTable("Category")->find($slot->category_id);
		$products = array();
		$category->getProducts($products, true);
		
		$index = 0;
		foreach ($products as $product)
		{
			$images = json_decode(html_entity_decode($product['images']), true);
			$_html .= '
				<div class="sale-items1">
					<img class="medium" src="'.$images['thumb'].'" />
					<span class="product-info">
						<li class="product-name">'.$product['name'].'</li>
						<a class="preview" href="'.url_for('product/view?product_id='.$product['id']).'">Preview</a>
					</span>	
				</div>
			';
			if (++$index >= 9) break;
		}

		echo $_html;
	}
	
	public static function displayProductBox($product)
	{
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		$images = json_decode(html_entity_decode($product['images']), true);
		$_html = '
			<div class="sale">
				<a href="'.url_for('product/view?product_id='.$product['id']).'">
					<img class="medium" src="'.$images['thumb'].'" />
				</a>
				<span class="product-info">
					<li class="product-name"><a href="'.url_for('product/view?product_id='.$product['id']).'">'.$product['name'].'</a></li>
					<li class="price"><span class="original_price">'.format_number((int)$product['original_price']).' $</span> - <span class="discount_price">'.format_number((int)$product['discount_price']).' $</span> </li>
				</span>	
			</div>';
		return $_html;
	}
	
	public static function displaySlotBox($slot)
	{
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		$sf_culture = sfContext::getInstance()->getUser()->getCulture();
		$category = Doctrine_Core::getTable("Category")->find($slot['category_id']);
		$_html = '
			<div class="sale">
				<h1><a href="'.url_for('category/view?category_id='.$category->id.'-'.vtnsSEO::getPermalink(($sf_culture == "vi")?$slot['display_name_vi']:$slot['display_name'])).'">'.(($sf_culture == "vi")?$slot['display_name_vi']:$slot['display_name']).'</a></h1>
				<img class="medium" src="'.$category->image.'" />
			</div>';
		return $_html;
	}
	
	public static function renderFooter()
	{
		$sf_culture = sfContext::getInstance()->getUser()->getCulture();
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		$_html = '';
		
		$q = Doctrine_Query::create()
				->from("VTNSCmsCategory cc")
				->where("cc.alias LIKE 'footer_%'")
				->addWhere("cc.parent_id = 0")
				->orderBy("cc.position")
				->limit(3);
		
		foreach ($q->fetchArray() as $category)
		{
			$qc = Doctrine_Query::create()
					->from("VTNSCmsCategory c")
					->where("c.parent_id = ?", $category['cms_category_id'])
					->orderBy("c.position");
			$_html .= '<ul class="nav-footer"><h1>'.(($sf_culture == "vi")?$category['name_vi']:$category['name']).'</h1>';
			foreach ($qc->fetchArray() as $child)
			{
				$_html .= '<li><a href="'.url_for('cms/permalink?cms_category_id='.$child['cms_category_id'].'-'.vtnsSEO::getPermalink(($sf_culture == "vi")?$child['name_vi']:$child['name'])).'">'.(($sf_culture == "vi")?$child['name_vi']:$child['name']).'</a></li>';
			}
			$_html .= '</ul>';
		}
		
		echo $_html;
	}
	
	public static function renderSocial()
	{
		$facebook = Doctrine_Core::getTable("Information")->findOneByAlias("facebook");
		$skype = Doctrine_Core::getTable("Information")->findOneByAlias("skype");
		$twitter = Doctrine_Core::getTable("Information")->findOneByAlias("twitter");
		$google = Doctrine_Core::getTable("Information")->findOneByAlias("google");
		$flickr = Doctrine_Core::getTable("Information")->findOneByAlias("flickr");
		$tumblr = Doctrine_Core::getTable("Information")->findOneByAlias("tumblr");
		$_html = '
			<ul class="social-icon">
				<h1>'.__('Connect with Us').'</h1>
				<li><a href="'.$facebook->content.'" target="_blank"><img src="/images/facebook.png" alt="Facebook" title="Facebook" /></a></li>
				<li><a href="'.$skype->content.'" target="_blank"><img src="/images/skype.png" alt="Skype" title="Skype" /></a></li>
				<li><a href="'.$twitter->content.'" target="_blank"><img src="/images/twitter.png" alt="Twitter" title="Twitter" /></a></li>
				<li><a href="'.$google->content.'" target="_blank"><img src="/images/google.png" alt="Google +" title="Google" /></a></li>
				<li><a href="'.$flickr->content.'" target="_blank"><img src="/images/flickr.png" alt="Flickr" title="Flickr" /></a></li>
				<li><a href="'.$tumblr->content.'" target="_blank"><img src="/images/tumblr.png" alt="Tumblr" title="Tumblr" /></a></li>
			</ul>
		';
		echo $_html;
	}
	
	public static function renderVideo()
	{
		$_html = '';
		$category = Doctrine_Core::getTable("VTNSCmsCategory")->findOneByAlias("video");
		$q = Doctrine_Query::create()
				->from("VTNSCms c")
				->where("c.cms_category_id = ?", $category->cms_category_id)
				->orderBy("c.created_at")
				->limit(1);

		foreach ($q->fetchArray() as $cms)
		{
			$_html .= vtnsMedia::video_embed($cms['link']);
		}
		
		echo $_html;
	}
	
	public static function renderCategoryMenu($category_id)
	{
		$_html = '<div class="category_menu">';
		$_html .= '<div class="category_menu_title">'.__('Category').'</div>';
		
		$_html .= vtnsSlot::displayCategoryMenu($category_id, 1);
		
		$_html .= '</div>';
		
		$_html .= '
			<script type="text/javascript" src="/js/menu.js"></script>
		';
		echo $_html;
	}
	
	public static function displayCategoryMenu($category_id, $parent_id)
	{
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		$_html = '';
		
		$q = Doctrine_Query::create()
				->from("Category c")
				->where("parent_id = ?", $parent_id)
				->addWhere("is_active = 1");
		if ($q->count() > 0)
		{
			$_html .= '<div class="category_menu_item_wrapper" '.(($parent_id == 1)?'style="display:block;"':'').'>';
			foreach ($q->fetchArray() as $category)
			{
				$_html .= '<div class="category_menu_item">';
				$_html .= '<a href="'.url_for('category/view?category_id='.$category['id'].'-'.vtnsSEO::getPermalink($category['name'])).'" '.(($category_id == $category['id'])?'class="cat_menu_item_active"':'').'>'.$category['name'].'</a>';
				$_html .= vtnsSlot::displayCategoryMenu($category_id, $category['id']);
				$_html .= '</div>';
			}
			$_html .= '</div>';
			if ($parent_id != 1)
				$_html .= '<span class="grower CLOSE"></span>';
		}
		
		return $_html;
	}
	
	public static function renderUserMenu($sf_guard_user)
	{
		$module_type = sfContext::getInstance()->getModuleName();
		$_html = '
			<div class="sidenav" style="width:100%">
				<div class="sidebar-widget" style="margin: -1px 0 0 0;">
                    <h5 class="title" style="margin-bottom:0">'.__("My user").'</h5>
                </div>
				<div class="mainnav">
					<ul>
						<li>
							<a '.(($module_type=="sfGuardUser")?'class="current"':'').' href="'.url_for("@sf_guard_user_object?id=".$sf_guard_user->id."&action=edit").'"><span class="icon16 entypo-icon-users"></span> My Profile</a>
						</li>
						<li>
							<a href="#"><span class="icon16 icomoon-icon-basket"></span> Orders</a>
						</li>
						<li>
							<a href="#" onclick="sign_out()"><span class="icon16 icomoon-icon-exit"></span> Logout</a>
						</li>
					</ul>
				</div>
			</div>
		';
		return $_html;
	}
}